//
//  XVCRegionalShowViewController.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/5/25.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCRegionalShowViewController.h"
#import "XXImageShowCell.h"
#import "XXTool.h"

@interface XVCRegionalShowViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *contentView;

@property (nonatomic,strong) DKButton *leftTurnButton;

@property (nonatomic,strong) DKButton *rightTurnButton;

@property (nonatomic,strong) NSMutableArray *imgShowArray;

@end

@implementation XVCRegionalShowViewController
{
    NSInteger imgIndex;
    NSMutableArray <XMLRegionalShow *> *dataArray;
}
-(UIScrollView*)contentView
{
    if (_contentView==nil)
    {
        _contentView = [[UIScrollView alloc] init];
        _contentView.delegate = self;
        _contentView.showsHorizontalScrollIndicator = NO;
        _contentView.pagingEnabled = YES;
        [self.view addSubview:_contentView];
    }
    return _contentView;
}

-(DKButton*)leftTurnButton
{
    if (_leftTurnButton==nil)
    {
        _leftTurnButton = [[DKButton alloc] init];
        _leftTurnButton.image = [UIImage imageNamed: @"白色箭头左"];
        [_leftTurnButton setColor:[UIColor clearColor] ForState:DKButtonStateNormal];
        [_leftTurnButton addTarget:self Select:@selector(clickLeftButton) ForEvent:DKButtonEventPressedEnd];
        _leftTurnButton.imageSize = CGSizeMake(37, 50);
        _leftTurnButton.imageRenderingMode = DKButtonRenderingModeAlwaysOriginal;
        _leftTurnButton.hidden = YES;
        [self.view insertSubview:_leftTurnButton atIndex:self.view.subviews.count];
    }
    return _leftTurnButton;
}

-(DKButton*)rightTurnButton
{
    if (_rightTurnButton==nil)
    {
        _rightTurnButton = [[DKButton alloc] init];
        _rightTurnButton.image = [UIImage imageNamed:@"白色箭头右"];
        _rightTurnButton.imageSize = CGSizeMake(37, 50);
        [_rightTurnButton setColor:[UIColor clearColor] ForState:DKButtonStateNormal];
        [_rightTurnButton addTarget:self Select:@selector(clickRightButton) ForEvent:DKButtonEventPressedEnd];
        _rightTurnButton.imageRenderingMode = DKButtonRenderingModeAlwaysOriginal;
        [self.view insertSubview:_rightTurnButton atIndex:self.view.subviews.count];
    }
    return _rightTurnButton;
}

-(void)initParameter
{
    imgIndex = 0;
}

-(void)initUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.thisType==AreaShow)
    {
        [self.navigationItem setTitle:NSLocalizedString(@"地区展会", nil)];
    }
    else
    {
        
    }
    [self contentView];
    [self imgShowArray];
}

-(void)initData
{
    NSLog(@"0");

    if (self.thisType==AreaShow)
    {
        [XMLRegionalShow requestRegionalUseDataWithCompletion:^(BOOL is, NSMutableArray<XMLRegionalShow *> *data) {
            if (is)
            {
                dataArray = data;
                if (_imgShowArray==nil)
                {
                    _imgShowArray = [NSMutableArray array];
                }
                else
                {
                    for (int i=0; i<_imgShowArray.count; i++)
                    {
                        [_imgShowArray[i] removeFromSuperview];
                    }
                    [_imgShowArray removeAllObjects];
                }
                for (int i=0; i<dataArray.count; i++)
                {
                    XXImageShowCell *cell = [[XXImageShowCell alloc] init];
                    
                    [_contentView addSubview:cell];
                    [_imgShowArray addObject:cell];
                    [cell loadPhotoModel:dataArray[i]];
                }
                
                XMLRegionalShow *temp = dataArray[0];
                
                [self.navigationItem setTitle:temp.imgTitle];
                
                [self refreshLayout];
            }
        }];
    }
    else
    {
        dataArray = [NSMutableArray array];
        NSArray *imgUrlArray = [self.imgUrl componentsSeparatedByString:@","];
        for (int i=0; i<imgUrlArray.count; i++)
        {
            XMLRegionalShow *temp = [[XMLRegionalShow alloc] init];
            temp.imgUrl = imgUrlArray[i];
            temp.imgTitle = @"";
            [dataArray addObject:temp];
        }
        if (_imgShowArray==nil)
        {
            _imgShowArray = [NSMutableArray array];
        }
        else
        {
            for (int i=0; i<_imgShowArray.count; i++)
            {
                [_imgShowArray[i] removeFromSuperview];
            }
            [_imgShowArray removeAllObjects];
        }
        for (int i=0; i<dataArray.count; i++)
        {
            XXImageShowCell *cell = [[XXImageShowCell alloc] init];
            
            [_contentView addSubview:cell];
            [_imgShowArray addObject:cell];
            [cell loadPhotoModel:dataArray[i]];
        }
        
        XMLRegionalShow *temp = dataArray[0];
        
        [self.navigationItem setTitle:temp.imgTitle];
        
        [self refreshLayout];
    }
}

-(void)refreshLayout:(CGRect)frame
{
    self.contentView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    self.leftTurnButton.frame = CGRectMake(10
                                           , (frame.size.height-50)/2
                                           , 37
                                           , 50);
    self.rightTurnButton.frame = CGRectMake(frame.size.width-10-37
                                            , _leftTurnButton.frame.origin.y
                                            , _leftTurnButton.frame.size.width
                                            , _leftTurnButton.frame.size.height);
    for (int i=0; i<_imgShowArray.count; i++)
    {
        XXImageShowCell *temp = _imgShowArray[i];
        temp.frame = CGRectMake(i*frame.size.width
                                , 0
                                , frame.size.width
                                , frame.size.height);
    }
    _contentView.contentSize = CGSizeMake(frame.size.width*_imgShowArray.count, frame.size.height);
}

#pragma mark scrollviewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/self.view.frame.size.width;
    imgIndex = index;
    XMLRegionalShow *temp = dataArray[index];
    [self.navigationItem setTitle:temp.imgTitle];
    if (index==0)
    {
        _rightTurnButton.hidden = NO;
        _leftTurnButton.hidden = YES;
    }
    else if (index==dataArray.count-1)
    {
        _rightTurnButton.hidden = YES;
        _leftTurnButton.hidden = NO;
    }
    else
    {
        _rightTurnButton.hidden = NO;
        _leftTurnButton.hidden = NO;
    }
}

#pragma mark Action
-(void)clickLeftButton
{
    if (imgIndex==0)
    {
        return;
    }
    imgIndex--;
    _rightTurnButton.hidden = NO;
    [_contentView scrollRectToVisible:CGRectMake(imgIndex*_contentView.frame.size.width
                                                 , 0
                                                 , _contentView.frame.size.width
                                                 , _contentView.frame.size.height)
                             animated:YES];
    XMLRegionalShow *temp = dataArray[imgIndex];
    [self.navigationItem setTitle:temp.imgTitle];
    if (imgIndex==0)
    {
        _leftTurnButton.hidden = YES;
    }
    else
    {
        _leftTurnButton.hidden = NO;
    }
    
}

-(void)clickRightButton
{
    if (imgIndex==_imgShowArray.count-1)
    {
        return;
    }
    imgIndex++;
    _leftTurnButton.hidden = NO;
    [_contentView scrollRectToVisible:CGRectMake(imgIndex*_contentView.frame.size.width
                                                 , 0
                                                 , _contentView.frame.size.width
                                                 , _contentView.frame.size.height)
                             animated:YES];
    XMLRegionalShow *temp = dataArray[imgIndex];
    [self.navigationItem setTitle:temp.imgTitle];
    if (imgIndex==_imgShowArray.count-1)
    {
        _rightTurnButton.hidden = YES;
    }
    else
    {
        _rightTurnButton.hidden = NO;
    }
}

#pragma mark api
+(void)pushRegionalShowVCWithSourceVC:(UIViewController *)sourceVC
                         WithShowType:(RegionalShowViewControllerType)type
                               imgUrl:(NSString *)img
{
    XVCRegionalShowViewController *vc = [[XVCRegionalShowViewController alloc] init];
    
    vc.thisType = type;
    vc.imgUrl = img;
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

@end
